window.onload=function () {
	getmodels(GetQueryString('brand_id'),GetQueryString('brand_name'));
	getbrandname(GetQueryString('brand_id'));
}


function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}


function uploadmodel() {
	$.ajax({
		url: 'http://172.18.21.150/ToGo/addModelToBrand',
		type: 'post',
		dataType: 'json',
		data: {brand_id:GetQueryString('brand_id'),model_name:$('#model-name').val()},
		success:function (result) {
			if (result.status=='200') {
				$('#mid-brandlist').html("");
				getmodels(GetQueryString('brand_id'));
				$('#model-name').val("")
			}
		},
		error:function(){
			alert('error');
		}
	});
}

function getmodels(brand_id){
	$.ajax({
		url: 'http://172.18.21.150/ToGo/getModels',
		type: 'post',
		dataType: 'json',
		data: {brand_id: brand_id},
		success:function (result) {
			if(result.status=='200'){
				var models=result.data;
				for (var i = 0; i < models.length; i++) {
					$('#mid-brandlist').append('<li>'+models[i].name+'</li>');
				}
			}
		},
		error:function () {
			alert('error');
		}
	});	
}

function getbrandname(brand_id) {
	$.ajax({
		url: 'http://172.18.21.150/ToGo/getBrand',
		type: 'post',
		dataType: 'json',
		data: {brand_id: brand_id},
		success:function (result) {
			if(result.status=='200'){
			$('#mid-brand-name-h3').html(result.data.brand_name);
		}
		},
		error:function () {
			
		}
	});
}
